package net.wanho.controller;


import net.wanho.po.Role;
import net.wanho.service.RoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.wanho.vo.AjaxResult;
import net.wanho.vo.PermVo;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;



import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 汤小洋
 * @since 2025-09-25
 */
@Api(tags = "06.角色表接口")
@RestController
@RequestMapping("/roles")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @ApiOperation("角色表分页查询")
    @GetMapping("/list")
    public AjaxResult findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                              @ApiParam("页大小") @RequestParam(defaultValue = "5") int pageSize,
                              @ApiParam("查询条件") Role role){
        PageInfo<Role> pageInfo = roleService.findPage(pageNum,pageSize,role);
        return AjaxResult.success(pageInfo);
    }

    @ApiOperation("根据主键查询角色表")
    @GetMapping("/{roleId}")
    public AjaxResult findById(@PathVariable Serializable roleId){
        return AjaxResult.success(roleService.findById(roleId));
    }

    @ApiOperation("新增角色表")
    @PostMapping
    public AjaxResult add(@RequestBody Role role){
        roleService.add(role);
        return AjaxResult.success();
    }

    @ApiOperation("修改角色表")
    @PutMapping
    public AjaxResult modify(@RequestBody Role role){
        roleService.modify(role);
        return AjaxResult.success();
    }

    @ApiOperation("根据主键删除角色表")
    @DeleteMapping("/{roleId}")
    public AjaxResult removeById(@PathVariable Serializable roleId){
        roleService.removeById(roleId);
        return AjaxResult.success();
    }

    @ApiOperation("角色授权")
    @PostMapping("/{roleId}/rights")
    public AjaxResult grant(@PathVariable Integer roleId, @RequestBody Map<String, String> map){
        roleService.grant(roleId,map.get("rids"));
        return AjaxResult.success();
    }

    @ApiOperation("删除角色指定权限")
    @DeleteMapping("/{roleId}/rights/{rightId}")
    public AjaxResult removeRight(@PathVariable Integer roleId, @PathVariable Integer rightId){
        List<PermVo> list = roleService.removeRight(roleId,rightId);
        return AjaxResult.success(list);
    }
}
